		/*BigInteger val = new BigInteger("0000100011111111", 2);
		//Binary to String
		String test = new String(val.toByteArray()); 
		System.out.println(test);
		val = new BigInteger(test.getBytes());
		//String to Binary
		System.out.println(val.toString(2));
		//String to Binary String
		System.out.println(new BigInteger(test.getBytes()).toString(2));
		System.out.println("Size: " + new BigInteger(test.getBytes()).toString(2).length());*/

package mdp.rpi.communicator;

import java.io.BufferedWriter;
import java.io.DataInputStream;
import java.io.DataOutputStream;
import java.io.File;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.OutputStreamWriter;
import java.io.Writer;
import java.math.BigInteger;
import java.net.Socket;

public class RaspberryController {
	private static final int PORT = 5182, PORT2 = 9991;
	private static final String IP_ADDRESS = "192.168.2.2", IP_ADDRESS2 = "172.20.175.24", FILE_PATH = System.getProperty("user.home") + "/Desktop/Communication_Log.txt";
	
	private Socket socket;
	private String syntax, commsLog = "";
	private DataInputStream dataInputStream;
	private DataOutputStream dataOutputStream;

	File statText;
	FileOutputStream is;
	OutputStreamWriter osw;
	Writer w; 
	
	public RaspberryController() throws NullPointerException, IOException, FileNotFoundException {		
		statText = new File(FILE_PATH);
        is = new FileOutputStream(statText);
        osw = new OutputStreamWriter(is);    
        w = new BufferedWriter(osw);
		socket = new Socket(IP_ADDRESS, PORT);
		dataOutputStream = new DataOutputStream(socket.getOutputStream());
		dataInputStream = new DataInputStream(socket.getInputStream());
		commsLog += "Connection Establised.\r\n";
	}
	
	public RaspberryController(boolean vpn) throws NullPointerException, IOException, FileNotFoundException {
		statText = new File(FILE_PATH);
        is = new FileOutputStream(statText);
        osw = new OutputStreamWriter(is);    
        w = new BufferedWriter(osw);
		socket = new Socket(IP_ADDRESS2, PORT2);
		dataOutputStream = new DataOutputStream(socket.getOutputStream());
		dataInputStream = new DataInputStream(socket.getInputStream());
		commsLog += "Virtual Private Connection Established.\r\n";
	}
	
	public void sendMessage(String message) {
		try {
			byte[] buffer = message.getBytes();
			dataOutputStream.write(buffer);
			commsLog += "\r\nCommand Sent: " + message;
			System.out.print("[BEGIN]\"" + message + "\"[END OF MESSAGE] successfully sent to Raspberry Pi.\n");
		 }
		 catch(Exception e) {
			 System.out.print("Warning: The following message was not sent!\n");
			 System.out.print("\n[BEGIN]\"" + message + "\"[END OF MESSAGE]\n");
			 commsLog += "\r\nError Sending Command: " + message;
		 }
	}
	
	public String listen() {
		try {
			byte[] buffer = new byte[2048];
			int bytes;
			bytes = dataInputStream.read(buffer);
			syntax = new String(buffer,0,bytes);
			if(syntax.startsWith("H:")) {
				System.out.println("Command Received from Robot: " + syntax + "\n");
				commsLog += "\r\nCommand Received: " + syntax;
				syntax = syntax.substring(5, 18);
				System.out.println("Decoded Syntax: " + syntax + "\n");
			} else {
				commsLog += "\r\nCommand Received: " + syntax;
				System.out.println("Command Received: " + syntax + "\n");
			}
		} catch (Exception e) {
			disconnect();
		}
		return syntax;
	}
	
	public void disconnect() {
		try {
			if (dataOutputStream != null) {
				dataOutputStream.close();
				dataOutputStream = null;
			}
			if (dataInputStream != null) {
				dataInputStream.close();
				dataInputStream = null;
			}
			if (socket != null) {
				socket.close();
				socket = null;
			}
			commsLog += "\r\nConnection Disconnected.";
			closeCommLog();
		} catch (Exception e) {
			
		}
	}
	
	public void closeCommLog() {
		try {
	        w.write(commsLog);
			w.close();
		} catch (IOException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
	}
}